package classics.DP;

import java.util.Scanner;

public class Exwang {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int [][] num= new int[n][m];
        int [][] dp= new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                num[i][j] = scanner.nextInt();
                if(i==0&&j==0){
                    continue;
                }
                int left = Integer.MAX_VALUE;
                int up = Integer.MAX_VALUE;
                if(i>0){
                    int add = 0;
                    if(num[i-1][j]==num[i][j]){
                        add = 1;
                    }else{
                        add = 2;
                    }
                    left = dp[i-1][j]+add;
                }
                if(j>0){
                    int add = 0;
                    if(num[i][j-1]==num[i][j]){
                        add = 1;
                    }else{
                        add = 2;
                    }
                    up = dp[i][j-1]+add;
                }
                dp[i][j] = Math.min(up,left);
            }
        }
        System.out.println(dp[n-1][m-1]);
    }
}
